package com.ssm.wechat.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.pre.util.SignUtil;
import com.ssm.wechat.service.WechatService;

/**
 * 处理微信端的请求，是微信与后台对接的接口
 * 微信用户发送消息，微信平台接收并进行转发给我的后台
 * 只要是微信请求都要经过这个类
 * 这是微信公众号里面基本配置的接口
 * @author mps
 *
 */
@Controller
public class WechatController {

	@Autowired
	private WechatService wechatService;
	
	/**
	 * 微信提交接收
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/wechat/WechatPost")
	public void WechatPost(HttpServletRequest request, HttpServletResponse response) throws Exception{
		/**
		 * 1.确认请求来自微信服务器
		 */
		String signature = request.getParameter("signature");// 微信加密签名
		String timestamp = request.getParameter("timestamp");// 时间戳
		String nonce = request.getParameter("nonce");// 随机数
		String echostr = request.getParameter("echostr");// 随机字符串
		PrintWriter out = response.getWriter();
		// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
		if (SignUtil.checkSignature(signature, timestamp, nonce))
			out.print(echostr);
		/**
		 * 2.处理微信服务器发来的消息
		 */
		request.setCharacterEncoding("UTF-8");// 将请求、响应的编码均设置为UTF-8（防止中文乱码）
		response.setCharacterEncoding("UTF-8");
		String respMessage = wechatService.WechatReply(request);//调用核心业务类接收消息、处理消息
		// 响应消息
		PrintWriter out1 = response.getWriter();
		out1.print(respMessage);
		out1.close();
	}
}
